package com.sbox.widget;

import java.util.ArrayList;
import java.util.List;

import com.sbox.album.R;
import com.sbox.album.model.AlbumInfo;
import com.sbox.album.model.AlbumManager;
import com.sbox.util.AlbumLog;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AlbumListView extends ListView {
	private AlbumInfo[] mAlbums = null;
	private AlbumAdapter mAdapter = null;
	
	public AlbumListView(Context context) {
		super(context);
		init();
	}

	public AlbumListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}


	public AlbumListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);		
		init();
	}
	
	private void init()
	{
		this.setDrawingCacheEnabled(false);
	}
	
	public void loadAlbums()
	{			
		mAlbums = AlbumManager.listAlbums();
		
		mAdapter = new AlbumAdapter(this.getContext());
		mAdapter.addAlbums(mAlbums);
		
		this.setAdapter(mAdapter);
	}
	
	
	public class ImageHolder
	{
		public ImageView Image;
		public TextView Text;
	}

	public class AlbumAdapter extends BaseAdapter {
		
		private List<AlbumInfo> mAlbums = new ArrayList<AlbumInfo>();
		private Context mContext = null;
		private LayoutInflater mInflater = null;
		
		public AlbumAdapter(Context c)
		{
			mContext = c;
			mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		public void addAlbums(AlbumInfo[] albums)
		{
			if( albums == null )
				return;
			
			for( AlbumInfo album: albums )
				mAlbums.add(album);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mAlbums.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mAlbums.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			AlbumInfo info = null;
			ImageHolder holder = null;
			RelativeLayout layout = null;
			
			info = mAlbums.get(position);
			if( info == null )
				return null;
			
			if( convertView != null )
			{
				AlbumLog.info("convertView != null");
				holder = (ImageHolder)convertView.getTag();
			}
			else
			{
				holder = new ImageHolder();
				
				layout = (RelativeLayout)mInflater.inflate(R.layout.album_item, null);
				holder.Image = (ImageView)layout.findViewById(R.id.album_image);
				holder.Text = (TextView)layout.findViewById(R.id.album_name);
				
				layout.setTag(holder);
			}
			
			holder.Text.setText(info.getName());
			
			return layout;
		}
	}
}